package org.example.cet.demos.controller;


import jakarta.validation.Valid;
import org.example.cet.demos.converter.converter;
import org.example.cet.demos.domain.PageBean;
import org.example.cet.demos.domain.Result;
import org.example.cet.demos.domain.form.SwiperForm;
import org.example.cet.demos.domain.po.Swiper;
import org.example.cet.demos.domain.query.SwiperQuery;
import org.example.cet.demos.service.impl.SwiperServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hq
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/swiper")
public class SwiperController {
    @Autowired
    private SwiperServiceImpl swiperService;
    @Autowired
    private converter converter;

    @PostMapping("/add")
    public Result add(@Valid @RequestBody SwiperForm swiperForm) {
        Swiper swiper = converter.ToSwiper(swiperForm);
        swiperService.save(swiper);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        swiperService.removeById(id);
        return Result.success();
    }

    @PutMapping("/update/{id}")
    public Result update(@PathVariable Integer id, @Valid @RequestBody SwiperForm swiperForm) {
        Swiper swiper = converter.ToSwiper(swiperForm);
        swiper.setId(id);
        swiperService.updateById(swiper);
        return Result.success();
    }

    @GetMapping("/getPage")
    public Result getPage(SwiperQuery swiperQuery) {
        PageBean pb = swiperService.getPage(swiperQuery);
        return Result.success(pb);
    }

    @GetMapping("/getList")
    public Result getList() {
        return Result.success(swiperService.getList());
    }
}
